// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.highcharts");
yepjs.require("YBA.Plugin.superSlide");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Data.Statistics");


// 系统首页
yepjs.define("index", {

    NS: "YBA.Logic",

    field: function() {

        var Biz = {
            // 初始化事件
            init: function() {
                for (var i in Biz.event) {
                    Biz.event[i]();
                }
            },
            //  公共变量
            vars: {
                // 模板列表
                tpl: (function(){
                    var tpl = {};
                    $("script[type='text/ysmart']").each(function(){
                        var t = $(this);
                        tpl[t.attr("data-name")] = t.html();
                    });
                    return tpl;
                })(),
                //暂定空参数
                data:{}
            },
            // 页面元素
            el: {
                jpieStorage:$("#j-pieStorage"),
                jpieDistribution:$("#j-pieDistribution"),
                jlist:$("#j-list"),
                terminalList:$(".terminal-list"),
                jindexPie:$("#j-indexPie"),
                jindexActive:$("#j-indexActive"),
                jwholeBtn:$(".j-wholeBtn")
            },
            // 功能函数集合
            fn: {
                //创建圆形统计图，存储容量概况统计图 ||终端存储容量分布
                setStoragePie:function(dom,json){
                    dom.highcharts({
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false,
                            type: 'pie'
                        },
                        credits:{
                            text: '',
                            href: ''
                        },
                        title: {
                            text: ''
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                                    style: {
                                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                    }
                                }
                            }
                        },
                        series: [{
                            name: "百分比",
                            colorByPoint: true,
                            data: json
                        }]
                    });
                },
                //整体概况统计
                statisticsFirstPage:function(callback){
                    YBA.Data.Statistics.statisticsFirstPage({},function(json){
                        if(json.code == 200) {
                            var caseData = {
                                "memoryCase":[{
                                    "name":"可用",
                                    "y":json.memoryCase.availableSpaceSize
                                },{
                                    "name":"已用",
                                    "y":json.memoryCase.usedSpacePercent
                                }],
                                "storageCase":[]
                            };
                            var totalCapacity = 100;
                            for(j=0;j<json.storageCase.length;j++){
                                var dataSave = {
                                    "name":"",
                                    "y":""
                                }
                                dataSave.name = json.storageCase[j].name;
                                dataSave.y = parseFloat(json.storageCase[j].totalAccounting)*100;
                                caseData.storageCase[j] = dataSave;
                                totalCapacity = totalCapacity - dataSave.y;
                            }
                            caseData.storageCase[caseData.storageCase.length] = {
                                "name":"剩余可分配容量",
                                "y":totalCapacity
                            }
                            Biz.fn.setStoragePie(Biz.el.jpieStorage,caseData.memoryCase);
                            Biz.fn.setStoragePie(Biz.el.jpieDistribution,caseData.storageCase);

                            callback&&callback(json);
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: json.desc});
                        }
                    })
                },
                //获取终端存储容量分布情况
                storageTrends:function(data,callback){
                    YBA.Data.Statistics.storageTrends(data,function(json){
                        if(json.code == 200) {
                            callback&&callback(json);
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: json.desc});
                        }
                    })
                },
                //获取终端详情
                storageTrends:function(data,callback){
                    YBA.Data.Statistics.storageTrends(data,function(json){
                        if(json.code == 200) {
                            callback&&callback(json);
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: json.desc});
                        }
                    })
                }
            },
            // 初始化时执行的所有事件
            event: {
                //首页初始化
                indexInit:function(){
                    Biz.fn.statisticsFirstPage(function(json){
                        Biz.el.jlist.html(YBA.Common.ysmart(Biz.vars.tpl.listTpl, json.storageCase));
                        Biz.el.terminalList.slide({
                            effect:"left",
                            vis:7
                        });
                    });
                },
                //终端点击加载终端信息
                terminalClick:function(){
                    Biz.el.jlist.delegate(".j-terminalBtn","click",function(){
                        var t = $(this), li = t.closest('li') , id = t.attr("data-id");
                        Biz.el.jindexPie.hide();
                        Biz.el.jindexActive.css("width","100%");
                        Biz.el.jwholeBtn.removeClass('active');
                        li.addClass('active').siblings('li').removeClass('active');
                        Biz.fn.storageTrends({"terminalId":id},function(json){
                        	console.log(json)
                            var caseData = [];
                        	var totalCapacity = 100;
                            for(j=0;j<json.terminalMemoryCase.length;j++){
                                var dataSave = {
                                    "name":"",
                                    "y":""
                                }
                                dataSave.name = json.terminalMemoryCase[j].name;
                                dataSave.y = parseFloat(json.terminalMemoryCase[j].totalAccounting)*100;
                                caseData[j] = dataSave;
                                totalCapacity = totalCapacity - dataSave.y;
                            }
                            caseData[caseData.length] = {
                                "name":"剩余可分配容量",
                                "y":totalCapacity
                            }
                            Biz.fn.setStoragePie(Biz.el.jpieStorage,caseData);
                        })
                    })
                },
                //整体概况点击加载信息
                wholeClick:function(){
                    Biz.el.jwholeBtn.bind("click",function(){
                        Biz.el.jindexPie.show();
                        Biz.el.jindexActive.css("width","50%");
                        Biz.el.jwholeBtn.addClass('active');
                        $(".j-terminalBtn").closest("li").removeClass("active");
                        Biz.fn.statisticsFirstPage();
                    })
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
